<?php
class JL_upload{
	public $types = array(
		'image'=>'image/jpg,image/pjpg,image/jpeg,image/pjpeg,image/jpe,image/png,image/gif',
		'doc'=>'text/plain,application/msword,application/octet-stream,wps',
		'zip'=>'application/x-rar-compressed,zip,tar,gz,z,7z'
		);
	public $dir;
	public $type=array();
	public $size_mini;
	public $size_max;
	
	function __construct($dir, $types, $ext_type='', $size_mini = 1000,$size_max = 1000000){
		if (!is_dir($dir)) {
			throw new Exception("upload dir error",4001);
		}
		
		$this->dir=$dir;
		
		if ($types!='') {
			$this->type=array_merge($this->type,explode(',',$this->types[$types]));
		}
		if ($ext_type!='') {
			$this->type=array_merge($this->type,explode(',',$ext_type));
		}
		
		$this->size_mini=$size_mini;
		$this->size_max=$size_max;
		
	}
	
	function upload__($upfile,$dest_name){

		if (empty($upfile)) {
			throw new Exception("no file",4002);
		}
		if (empty($dest_name)) {
			throw new Exception("no destination",4003);
		}
//		var_dump($upfile['type']);exit;
//		var_dump($this->type);
//		var_dump($upfile['type']);exit;
		if (!in_array($upfile['type'],$this->type)) {
			throw new Exception("File type not allowed",4011);
		}
		
		if ($upfile['size'] < $this->size_mini) {
			throw new Exception('File is too mini',4012);
		}
		if ($upfile['size'] > $this->size_max) {
			throw new Exception('File is too max',4013);
		}
				
		if (!is_uploaded_file($upfile['tmp_name'])) {
			throw new Exception("file not HTTP POST upload",4021);
		}

		$this->dir=rtrim($this->dir,"/\\");
		$this->dir.="/";
		if (!move_uploaded_file($upfile['tmp_name'],$this->dir.$dest_name)){
			throw new Exception("Unable to move upload file",4022);
		}
		
		return $this->dir.$dest_name;
		
	}
	
	
}

?>